/*
 * Isman Usoh<isman.usoh@gmail.com>
 * www.facebook.com/isman.usoh
 */
package com.isman.postcodeinfo;

import java.util.ArrayList;

import com.isman.postcodeinfo.database.PostCode;
import com.isman.postcodeinfo.database.PostCodeDataSource;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SearchView;
import android.widget.TextView;

public class MainActivity extends Activity implements SearchView.OnQueryTextListener,OnClickListener {
	private String str;
	private Button mButton;
	private ListView mListView;
	private SearchView mSearchView;
	private PostCodeAdapter mAdepter;
	private PostCodeDataSource mDataSource;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		
		this.mListView = (ListView) findViewById(R.id.mainlist);

		
		this.mDataSource = new PostCodeDataSource(this);
		this.mDataSource.open();
		
		this.mButton = new Button(this);
		this.mButton.setText("no item");
		this.mButton.setEnabled(false);
		this.mButton.setOnClickListener(this);
		
		this.mAdepter = new PostCodeAdapter(this);
		this.mListView.addFooterView(this.mButton);
		this.mListView.setAdapter(this.mAdepter);
		
		
		this.str = "";
		this.mAdepter.setPostCodes(this.mDataSource.search(str));
		
		if(10 > this.mAdepter.getCount()){
			mButton.setText("more");
			mButton.setEnabled(false);
		}else if(10 == this.mAdepter.getCount()){
			mButton.setText("more");
			mButton.setEnabled(true);
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.activity_main, menu);
		MenuItem searchItem = menu.findItem(R.id.action_search);
		this.mSearchView = (SearchView) searchItem.getActionView();
		this.mSearchView.setOnQueryTextListener(this);
		return true;
	}

	public boolean onQueryTextChange(String str) {
		this.str = str;
		this.mAdepter.setPostCodes(this.mDataSource.search(str));
		
		Log.i("Isman", "Size : "+this.mAdepter.getCount());
		
		if(10 > this.mAdepter.getCount()){
			mButton.setText("more");
			mButton.setEnabled(false);
		}else if(10 == this.mAdepter.getCount()){
			mButton.setText("more");
			mButton.setEnabled(true);
		}
		
		
		Log.i("Isman", "Change " + str);
		return true;
	}

	public boolean onQueryTextSubmit(String str) {
		this.str = str;
		this.mAdepter.setPostCodes(this.mDataSource.search(str));
		
		if(10 > this.mAdepter.getCount()){
			mButton.setText("more");
			mButton.setEnabled(false);
		}else if(10 == this.mAdepter.getCount()){
			mButton.setText("more");
			mButton.setEnabled(true);
		}
		
		
		Log.i("Isman", "Submit " + str);
		return true;
	}

	public static class PostCodeAdapter extends BaseAdapter {
		ArrayList<PostCode> mPostCodes = null;
		private Context mContext;

		public PostCodeAdapter(Context mContext) {
			this.mContext = mContext;
			this.mPostCodes = new ArrayList<PostCode>();
		}

		public void setPostCodes(ArrayList<PostCode> mPostCodes) {
			this.mPostCodes.clear();
			this.mPostCodes.addAll(mPostCodes);
			this.notifyDataSetChanged();
		}
		
		public void addPostCodes(ArrayList<PostCode> mPostCodes) {
			this.mPostCodes.addAll(mPostCodes);
			this.notifyDataSetChanged();
		}
		
		public void addPostCodes(PostCode mPostCode) {
			this.mPostCodes.add(mPostCode);
			this.notifyDataSetChanged();
		}

		public int getCount() {
			return mPostCodes.size();
		}

		public Object getItem(int position) {
			return mPostCodes.get(position);
		}

		public long getItemId(int position) {
			return 0;
		}

		public View getView(final int position, View convertView, ViewGroup parent) {
			View view = LayoutInflater.from(mContext).inflate(
					R.layout.item_postcode, parent, false);
			TextView title = (TextView) view.findViewById(R.id.tvTitle);
			TextView subtitle = (TextView) view.findViewById(R.id.tvSubTitle);
			TextView postcode = (TextView) view.findViewById(R.id.tvPostcode);

			final PostCode mPostCode = mPostCodes.get(position);
			title.setText(mPostCode.getAddress() + mPostCode.getDistrict());
			subtitle.setText(mPostCode.getProvince());
			postcode.setText(mPostCode.getPostcodel());
			
			
			view.setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					Intent intent = new Intent(v.getContext(), DetailActivity.class);
					intent.putExtra(DetailActivity.DATA, mPostCodes);
					intent.putExtra(DetailActivity.INDEX, position);
					v.getContext().startActivity(intent);
				}
			});
			return view;
		}
	}

	public void onClick(View v) {
		int before = mAdepter.getCount();
		this.mAdepter.addPostCodes(this.mDataSource.search(str, before, before+10));
		
		if(before == mAdepter.getCount() || (before+10)  >  mAdepter.getCount()){
			mButton.setText("more");
			mButton.setEnabled(false);
		}else{
			mButton.setText("more");
			mButton.setEnabled(true);
		}
	}
}
